#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<queue>
using namespace std;
 // Definition for a binary tree node.
  struct TreeNode {
      int val;
      TreeNode *left;
      TreeNode *right;
      TreeNode() : val(0), left(nullptr), right(nullptr) {}
      TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
      TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  };
 
class Solution {
public:
    bool isEvenOddTree(TreeNode* root) {
        int levelSize = 1;
        int level = 1;
        queue<TreeNode*> q;
        q.push(root);

        while (!q.empty())
        {
            int prev = level % 2 == 0 ? INT_MAX : INT_MIN;
            while (levelSize--)
            {
                TreeNode* front = q.front();
                int val = front->val;
                q.pop();
                if (front->left)
                {
                    q.push(front->left);
                }
                if (front->right)
                {
                    q.push(front->right);
                }

                if (level % 2 != val % 2)
                {
                    return false;
                }
                if (level % 2 == 0 && prev <= val || level % 2 == 1 && prev >= val)
                {
                    return false;
                }

                prev = val;
            }
            levelSize = q.size();
            level++;
        }
        return true;
    }
};